// pages/search.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    clearSearchClass: 'clearSearchHidden',
    hotSearch: [
      { word: '搜索引擎', icon: '/image/hot-2.png' },
      { word: 'JVM', icon: '' },
      { word: '参数同步', icon: '/image/hot-2.png' },
      { word: 'Solr', icon: '/image/hot-2.png' },
      { word: 'SpringBoot', icon: '' },
      { word: 'Java 设计模式', icon: '' },
      { word: 'Docker', icon: '' },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var temp = app.globalData.statusBar;
    temp.topNavTitle = '热搜榜';
    this.setData(
      {
        barStatus: temp
      }
    );
    var queryWord = app.globalData.queryWord || '';
    this.setData({
      queryWord: queryWord
    });

    if (queryWord && queryWord.length > 0) {
      this.setData({
        clearSearchClass: ''
      });
    } else {
      this.setData({
        clearSearchClass: 'clearSearchHidden'
      });
    }

    console.log('data ', this.data);

    this.loadHistorySearchWord();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

  , clickAndHome: function (event) {
    var word = this.data.queryWord || '';
    //keyword
    app.globalData.queryWord = word;
    console.log('clickAndHome', word);
    app.clickAndHome({
      keyword : word
    });
  }

  , clickAndBack: function (event) {
    app.clickAndBack(event);
  }

  , cancelSearch : function(event) {
      app.clickAndBack();
  }

  , searchArticles : function(event) {
    var word = event.detail.value;
    word = word || '';
    console.log(word);
    this.addSearchWord(word);
    this.goSearchByWord(word);
  },
  goSearchByWord: function (word) {
    word = word || '';
    //设置全局查询参数：关键字
    app.globalData.queryWord = word;

    wx.switchTab({
      url: '/pages/home/home', //传递参数
      success: function (e) {
        var page = getCurrentPages().pop();
        if (page == undefined || page == null) return;
        page.onLoad();
      }
    });
  }

  , clearSearchWordAndFocus : function() {
    console.log('clearSearchWordAndFocus');
    var word = '';
    //app.globalData.queryWord = word;
    this.setData({
      queryWord: word
    });
  }, 
  addSearchWord: function (w) {
    console.log('addSearchWord', w);
    if(!w) {
      return ;
    }
    var list = this.listSearchWord();
    var map = {};
    var rs = [];
    rs.push({
      word : w
    });
    map[w] = 1;

    for (var i in list) {
      var obj = list[i];
      if (obj && map[obj.word]) {
        continue;
      }
      rs.push(obj);
      map[obj.word] = 1;
    }
    if(rs && rs.length > 10) {
      rs = rs.slice(0, 10);
    }
    wx.setStorageSync('_oom_search_words', rs);
  },
  deleteSearchWord : function(w) {
    console.log('deleteSearchWord', w);
    if(!w) {
      return ;
    }
    var list = this.listSearchWord();
    var map = {};
    var rs = [];
    
    for(var i in list) {
      var obj = list[i];
      if(obj.word == w) {
        continue;
      }
      rs.push(obj);
    }
    wx.setStorageSync('_oom_search_words', rs);
    this.loadHistorySearchWord();
  },
  listSearchWord : function() {
    var list = wx.getStorageSync('_oom_search_words') || [];

    return list;
  },
  loadHistorySearchWord : function() {
    var words = this.listSearchWord() || [];
    console.log('loadHistorySearchWord', words);
    this.setData({
      historys : words
    });
  },
  clickWordAndSearch : function(e) {
    var w = e.currentTarget.dataset.word || '';
    console.log('clickWordAndSearch', w);
    this.goSearchByWord(w);
  }
  , clickWordAndDelete: function (e) {
    var w = e.currentTarget.dataset.word || '';
    console.log('clickWordAndDelete', w);
    this.deleteSearchWord(w);
  }
  , clearAllHistorys : function() {

    wx.setStorageSync('_oom_search_words', []);
    this.loadHistorySearchWord();
  }
})